// pages/recommend/recommend.js

// 获取应用实例
const app = getApp()
const api = require('../../../utils/util')
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},

    /**
     * 页面的初始数据
     */
    data: {
        imagesUrl: app.globalData.imagesUrl,
        siteUrl: app.globalData.siteUrl,
        // 搜素
        value: '',
        // 列表数据
        page_num : 10,
        enterprise: [],
        page : 1,
        typeCurrent:0,
        // 分类弹窗
        typePopList: [
           
        ],
        typePopShow: false,
        category:''
    },
    onLoad(options){
      let that = this
      if(options.category){
        this.setData({
          category: options.category
        });
        this.category(that, options.category)
      }else{
        this.category(that, '')
      }
      
      
    },
    onShow() {
      let that = this
      this.get_enterprise(that, 1)
    },
    
    get_enterprise(that, page){
      let param = {
        str: that.data.value,
        page_num: this.data.page_num,
        category: this.data.category
      }
      api.enterprise_list_page(param, page).then(function(res){
        if (res.data.length>0){
          that.setData({
            enterprise: page == 1 ? res.data : that.data.enterprise.concat(res.data),//合并数据
            page: page + 1
          })
        }else{
            wx.showToast({
              title: '没有更多数据了',
              ico: 'error'
            })
        }
        wx.hideLoading();
      })
    },
//获取分类
category(that, category){
    
  api.category_list().then(function(res){
    let typePopList = [];
  
    for(let i = 0; i< res.data.length; i++){
      typePopList.push({
        txt: res.data[i].title
      })
    }
     that.setData({
      typePopList
     },()=>{
      for(let i = 0 ; i < that.data.typePopList.length; i++ ){
        if(that.data.typePopList[i].txt == category){
           that.setData({
            typeCurrent: i 
           })
           console.log(i)
        }
      }
     })
  })
},
    onPullDownRefresh:function(){
      this.onRefresh();
    },
    onRefresh:function(){
        let that = this
        //导航条加载动画
        wx.showLoading();
        wx.showNavigationBarLoading();
        setTimeout(function () {
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          that.setData({
            page: 1
          })
          that.get_enterprise(that, 1)
          wx.stopPullDownRefresh();
          wx.hideLoading();
        }, 2000);
      },
// 分类弹窗
typeShowPopup() {
  console.log(1)
  this.setData({ typePopShow: true });
},

      onReachBottom:function(){
       wx.showLoading();
        // 加载更多，滑动到底部执行此方法
        let page = this.data.page ;
        console.log(page)
        let that = this
        this.get_enterprise(that, page)
        
    },
    onStickyScroll: function(){

    },
    onChange: function(e){
       this.setData({
         value: e.detail
       })
       if(e.detail == ''){
        this.onRefresh();
      }
    },
    onSearch(){
      let that = this
      that.setData({
        page: 1
      })
      that.get_enterprise(that, 1)
    }
})